let arr;
$(function () {
    $.ajax({
        url: "/js/city.json",
        success: function (data) {
            for (let i = 0; i < data.length; i++) {
                arr=data;
                let province = data[i].label;  //省
                let option = "<option value='" + province + "'>" + province + "</option>";
                $("[name='province']").append(option);
            } 
        }
    })

    $("[name='province']").change(function () {

        $("[name='city'] option:not(:first)").remove();
        $("[name='qu'] option:not(:first)").remove();
        let sheng =$(this).val();
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].label==sheng){
                let arrShi=arr[i].children;
                for (let j = 0; j < arrShi.length; j++) {
                    let shi=arrShi[j].label;  //城市
                    let option = "<option value='" + shi + "'>" + shi + "</option>";
                    $("[name='city']").append(option);
                }
                break;
            }
        }
    })

    $("[name=city]").change(function () {

        $("[name='qu'] option:not(:first)").remove();
        let sheng =$(this).prev().val();  //选中省份
        let shi =$(this).val();           //选中城市
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].label==sheng){
                let arrShi=arr[i].children;   //城市数组
                for (let j = 0; j < arrShi.length; j++) {
                    let arrQu=arrShi[j].children;   //该城市的区域数组
                    if (arrShi[j].label==shi){
                        for (let k = 0; k < arrQu.length; k++) {
                            let qu=arrQu[k].label;  //区域
                            let option = "<option value='" + qu + "'>" + qu + "</option>";
                            $("[name='qu']").append(option);
                        }
                        break;
                    }
                }
                break;
            }
        }
    })
})

